#!/usr/bin/env php
<?php

function get_cmd_gate_path(){
    
    $self = $_SERVER['argv'][0];
    $cwd = getcwd();
    $realpath = realpath($self);
    chdir('/');
    if(realpath($self)!=$realpath){
        $self = $cwd.'/'.$self;
    }
    chdir($cwd);

    while(is_link($self)){
        $link = readlink($self);
        if($link{0}=='/'){
            $self = $link;
        }else{
            $self = dirname($self).'/'.$link;
        }
    }

    $path_arr = explode('/',$self);

    for($i=count($path_arr)-1;$i>-1;$i--){
        if($path_arr[$i]=='.'){
            unset($path_arr[$i]);
        }elseif($path_arr[$i]=='..'){
            unset($path_arr[$i--], $path_arr[$i]);
        }
    }
    $self = implode('/',$path_arr);
    return $self;
}

if(isset($_SERVER['ECOS_ROOT'])){
    define('ROOT_DIR',$_SERVER['ECOS_ROOT']);
}else{
    $self = get_cmd_gate_path();
    define('ROOT_DIR',realpath(dirname(dirname(dirname($self)))));
}


if(file_exists(ROOT_DIR.'/app/base/kernel.php')){
    define('APP_DIR',ROOT_DIR.'/app');
}else{
    define('APP_DIR',dirname(dirname(__FILE__)));
}

require('kernel.php');


if(!kernel::register_autoload()){
    require('autoload.php');
}
$shell = new base_shell_loader;
$shell->run();